package com.logos.cartoon.service.impl;

import com.logos.cartoon.entity.CharacterLiked;
import com.logos.cartoon.enums.CharLikedStatusEnum;
import com.logos.cartoon.mapper.CharacterLikedMapper;
import com.logos.cartoon.redis.RedisUtil;
import com.logos.cartoon.req.CharacterLikedReq;
import com.logos.cartoon.service.CharacterLikedService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 角色点赞表(CharacterLiked)表服务实现类
 *
 * @author rose
 * @since 2025-03-18 17:17:04
 */
@Service("characterLikedService")
public class CharacterLikedServiceImpl implements CharacterLikedService {
    @Resource
    private CharacterLikedMapper characterLikedMapper;

    @Resource
    private RedisUtil redisUtil;

    private static final String CHAR_LIKED_KEY = "character:liked";

    public static final String RANK_KEY = "character:rank";

    private static final String CHAR_LIKED_COUNT_KEY = "character:liked:count";

    private static final String CHAR_LIKED_DETAIL_KEY = "character:liked:detail";

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CharacterLiked queryById(Long id) {
        return this.characterLikedMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param characterLiked 实例对象
     * @return 实例对象
     */
    @Override
    public CharacterLiked insert(CharacterLiked characterLiked) {
        this.characterLikedMapper.insert(characterLiked);
        return characterLiked;
    }

    /**
     * 修改数据
     *
     * @param characterLiked 实例对象
     * @return 实例对象
     */
    @Override
    public CharacterLiked update(CharacterLiked characterLiked) {
        this.characterLikedMapper.update(characterLiked);
        return this.queryById(characterLiked.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.characterLikedMapper.deleteById(id) > 0;
    }

    @Override
    public void add(CharacterLikedReq characterLikedReq) {
        Long characterId = characterLikedReq.getCharacterId();
        String likeUserId = characterLikedReq.getLikeUserId();
        Integer status = characterLikedReq.getStatus();
        String detailKey = CHAR_LIKED_DETAIL_KEY + ":" + characterId + ":" + likeUserId;
        String countKey = CHAR_LIKED_COUNT_KEY + ":" + characterId;
        if (CharLikedStatusEnum.LIKED.getCode() == status) {
            redisUtil.increment(countKey, 1);
            redisUtil.set(detailKey, "1");
            redisUtil.addScore(RANK_KEY, characterId, 1);
        } else {
            Double score = redisUtil.score(RANK_KEY, characterId);
            Integer count = redisUtil.getInt(countKey);
            if (Objects.isNull(count) || count <= 0 || Objects.isNull(score) || score.compareTo(0.0d) <= 0) {
                return;
            }
            redisUtil.addScore(RANK_KEY, characterId, -1);
            redisUtil.increment(countKey, -1);
            redisUtil.del(detailKey);
        }
    }
}
